package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.MchntProdInfoMapper;
import cc.rengu.igas.bsps.common.entity.MchntProdInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户产品信息表 T_B_MCHNT_PROD_INFO 操作实现
 * <p>
 * Created by keyi on 2020/3/26.
 */
public class MchntProdInfoMapperImpl implements MchntProdInfoMapper {

    RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public List<MchntProdInfo> selectOpmsMchntProdInfoByInstIdMchntNo(String instId, String mchntNo) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "opmsGetMchntProdInfoByInstIdMchntNo");

        MchntProdInfo info = new MchntProdInfo();
        info.setInstId(instId);
        info.setMchntNo(mchntNo);

        List<Object> objects = dbsUtil.dbsFetchLimitListData(sqlStr, info, MchntProdInfo.class);
        return objects.stream().map(item -> (MchntProdInfo) item).collect(Collectors.toList());
    }

    @Override
    public MchntProdInfo selectMchntProdInfoByPrimaryKey(String instId, String mchntNo, String baseProdCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntProdInfoByPrimaryKey");
        MchntProdInfo mchntProdInfo = new MchntProdInfo();
        mchntProdInfo.setInstId(instId);
        mchntProdInfo.setMchntNo(mchntNo);
        mchntProdInfo.setBaseProdCode(baseProdCode);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntProdInfo, mchntProdInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户产品信息失败,instId:<{}>,mchntNo:<{}>,baseProdCode:<{}>,iReturnCode:<{}>", instId, mchntNo, baseProdCode, iReturnCode);
            return null;
        }
        return mchntProdInfo;
    }
}
